package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import com.itheima.domain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author wuli
 * @create 2021-11-17 23:12
 */
@Controller

public class UserController {

    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public String save() {
        System.out.println("UserController runnig...");
        return "success";
    }

    @RequestMapping(value = "/save2", method = RequestMethod.GET)
    public ModelAndView save2() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        modelAndView.addObject("username", "itcast");

        return modelAndView;
    }

    @RequestMapping(value = "/save3", method = RequestMethod.GET)
    public ModelAndView save3(ModelAndView modelAndView) {
        modelAndView.setViewName("success");
        modelAndView.addObject("username", "itheima");

        return modelAndView;
    }

    @RequestMapping(value = "/save4", method = RequestMethod.GET)
    public String save4(Model model) {
        model.addAttribute("username", "博学谷");
        return "success";
    }

    @RequestMapping(value = "/save5", method = RequestMethod.GET)
    public String save5(HttpServletRequest request) {
        request.setAttribute("username", "酷丁鱼");
        return "success";
    }

    @RequestMapping(value = "/save6", method = RequestMethod.GET)
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello itheima");
    }

    @ResponseBody
    @RequestMapping(value = "/save7", method = RequestMethod.GET)
    public String save7() throws IOException {
        return "hello itheima";
    }

    @ResponseBody
    @RequestMapping(value = "/save8", method = RequestMethod.GET)
    public String save8() throws IOException {
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }

    @ResponseBody
    @RequestMapping(value = "/save9", method = RequestMethod.GET)
    public String save9() throws IOException {
        User user = new User();
        user.setUsername("lisi");
        user.setAge(30);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "/save10", method = RequestMethod.GET)
    public User save10() throws IOException {
        User user = new User();
        user.setUsername("lisi2");
        user.setAge(32);

        return user;
    }

    @ResponseBody
    @RequestMapping(value = "/save11", method = RequestMethod.GET)
    public void save11(String username, Integer age) throws IOException {
        System.out.println(username);
        System.out.println(age);
    }

    @ResponseBody
    @RequestMapping(value = "/save12", method = RequestMethod.GET)
    public void save12(User user) throws IOException {
        System.out.println(user);
    }

    @ResponseBody
    @RequestMapping(value = "/save13", method = RequestMethod.GET)
    public void save13(User user) throws IOException {
        System.out.println(user);
    }

    @ResponseBody
    @RequestMapping(value = "/save14", method = RequestMethod.POST)
    public void save14(VO vo) throws IOException {
        System.out.println(vo);
    }

    @ResponseBody
    @RequestMapping(value = "/save15")
    public void save15(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);
    }

    @ResponseBody
    @RequestMapping(value = "/save16")
    public void save16(@RequestParam("name") String username) throws IOException {
        System.out.println(username);
    }

    @ResponseBody
    @RequestMapping(value = "/save17/{username}")
    public void save17(@PathVariable("username") String username) throws IOException {
        System.out.println(username);
    }

    @ResponseBody
    @RequestMapping(value = "/save18")
    public void save18(Date date, String username) throws IOException {
        System.out.println(date);
        System.out.println(username);
    }

    @ResponseBody
    @RequestMapping(value = "/save19")
    public void save19(String username, MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        System.out.println(uploadFile);
    }

    @RequestMapping(value = "/save20")
    public String save20(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.getWriter().print("aaaaaaaaa");
        String accept = request.getHeader("accept");
        System.out.println(accept);
        System.out.println(request.getHeader("X-Requested-With"));
        return "success";
    }
}
